<style lang="scss" scoped>
</style>
<template>
    <section class="container0">
       
    </section>
</template>
<script>
    import UserDao from '@/api/UserDao';
    import baseUtil from "../../utils/baseUtil";
    export default {
        data() {

            var checkRegUser= (rule, value, callback) => {
                if (this.isExist == true)  {
                    callback(new Error('用户名已存在'))
                } else {
                    callback()
                }
            }
            var checkForgetUser= (rule, value, callback) => {
                if (this.isExist == false)  {
                    callback(new Error('用户名不存在,请到注册页面进行注册'))
                } else {
                    callback()
                }
            }
            var regPwd2= (rule, value, callback) => {
                if (!baseUtil.isNotBlank(value) ) {
                    callback(new Error('请再次输入密码!'))
                } else if (value !== this.regForm.passWord) {
                    callback(new Error('密码不一致!'))
                } else {
                    callback()
                }
            }
            var forgetPwd2= (rule, value, callback) => {
                if (!baseUtil.isNotBlank(value) ) {
                    callback(new Error('请再次输入密码!'))
                } else if (value !== this.forgetForm.passWord) {
                    callback(new Error('密码不一致!'))
                } else {
                    callback()
                }
            }
            return {
                dialogFormVisible: true,
				loginForm:{
                    username:localStorage.userName != null?localStorage.userName:null,
					password:localStorage.password != null?localStorage.password:null,
				},
                regForm: {
		          userName: null,
		          passWord: null,
				  confirmPwd:null
		        },
                forgetForm: {
                    userName: null,
                    passWord: null,
                    confirmPwd:null
                },
				editUser:null,
                isExist: true,
                checkPassword:localStorage.checkPassword != null?localStorage.checkPassword == 'true'?true:false:null,
				mode:0,
				rulesLogin:{
                    username:[
                        {required: true, message: '请输入姓名!', trigger: 'blur'},
                    ],
                    password:[
                        {required: true, message: '请输入密码!', trigger: 'blur'},
                    ],
				},
                rulesReg:{
                    userName:[
                        {required: true, message: '请输入姓名!', trigger: 'blur'},
                        {min: 2, max: 10, message: '长度在 2 到 10 个字符!'},
                        {pattern: /^[\u4E00-\u9FA50-9]+$/, message: '只能输入中文或数字!'},
                        {validator:checkRegUser}
					],
                    passWord:[
                        {required: true, message: '请输入密码!', trigger: 'blur'},
                        {pattern: /^[a-zA-Z0-9]{6,16}$/, message: '密码由字母或数字，6-16位组成!'}
					],
                    confirmPwd:[
                         {required: true, trigger: 'blur', validator:regPwd2}
					]
				},
                rulesForget:{
                    userName:[
                        {required: true, message: '请输入姓名!', trigger: 'blur'},
                        {min: 2, max: 10, message: '长度在 2 到 10 个字符!'},
                        {pattern: /^[\u4E00-\u9FA50-9]+$/, message: '只能输入中文或数字!'},
                        {validator:checkForgetUser}
                    ],
                    passWord:[
                        {required: true, message: '请输入密码!', trigger: 'blur'},
                        {pattern: /^[a-zA-Z0-9]{6,16}$/, message: '密码由字母或数字，6-16位组成!'}
                    ],
                    confirmPwd:[
                        {required: true, trigger: 'blur', validator:forgetPwd2}
                    ]
                },
            }
        },
        methods: {
            rememberMeOrNot() {
                if(this.checkPassword) {
                    localStorage.userName = this.loginForm.username
                    localStorage.password = this.loginForm.password
                }else {
                    this.loginForm.username = null;
                    this.loginForm.password = null;
                    localStorage.removeItem('userName');
                    localStorage.removeItem('password');
                }
                localStorage.checkPassword = this.checkPassword;
            },
            switchMode(mode) {
                switch(mode){
					case 0:
                        this.loginForm = {username:null,password:null}
                        this.resetForm('loginForm')
					    break;
                    case 1:
                        this.regForm = {userName:null, passWord:null,confirmPwd:null}
                        this.resetForm('regForm')
                        break;
                    case 2:
                        this.forgetForm = {userName:null, passWord:null,confirmPwd:null}
                        this.resetForm('forgetForm')
                        break;
				}
                this.mode = mode;
			},
            checkRegUser(){
                 UserDao.findListNoPage({name:this.regForm.userName}).then((m)=>{
                  if(this.baseUtil.isNotBlankObj(m) && m.length>0){
                      this.isExist = true
				  }else {
                      this.isExist = false
				  }
				 })
		   },
			checkForgetUser(){
				UserDao.findListNoPage({name:this.forgetForm.userName}).then((m)=>{
					if(this.baseUtil.isNotBlankObj(m) && m.length>0){
                        this.editUser = m
						this.isExist = true
					}else {
						this.isExist = false
					}
				})
			},
           submitBtn(formName) {
                if(this.checkPassword) {
                    localStorage.userName = this.loginForm.username
                    localStorage.password = this.loginForm.password
                }else {
                    localStorage.removeItem('userName');
                    localStorage.removeItem('password');
                }
                localStorage.checkPassword = this.checkPassword;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
						switch(this.mode){
							case 0:   //登录
                                UserDao.findListNoPage({$and:[{name:this.loginForm.username},{password:this.$md5(this.loginForm.password)}]}).then((m)=>{
                                    if(this.baseUtil.isNotBlankObj(m) && m.length>0){
                                        this.$message({
                                            message: '登录成功',
                                            type: 'success',
                                        });
                                        this.$store.dispatch('LoginAction', {username:this.loginForm.username});
                                        this.$store.dispatch('PushCurrentMenu', 1);
                                        this.$router.push({ path: '/receiptList' })
                                        // this.dialogFormVisible = false
                                    }else {
                                        this.$message({
                                            message: '用户名或密码错误',
                                            type: 'error',
                                        });
                                    }
                                }).catch(()=>{
                                    this.$message({
                                        message: '用户名或密码错误',
                                        type: 'error',
                                    });
                                })
                                break;
							case 1:
							 let user = {name:this.regForm.userName,password:this.$md5(this.regForm.passWord),createAt:new Date(),deleted:0}  //md5加密，应该再加盐
							     UserDao.insertData(user)
                                this.$message({
                                    message: '注册成功',
                                    type: 'success',
                                });
                                this.switchMode(0)
                                break;
							case 2:  //修改
							     UserDao.updateData({name:this.forgetForm.userName},{password:this.$md5(this.forgetForm.passWord)}).then((m)=>{
                                         this.$message({
                                             message: '修改成功',
                                             type: 'success',
                                         });
                                         this.switchMode(0)
								 }).catch(()=>{
                                     this.$message({
                                         message: '修改失败',
                                         type: 'err',
                                     });
								 })
							    break;
						}
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
		mounted(){
		}
    }
</script>